<template>
  <div class="container" ref="mainCon">
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="所属区域" class="row-padding-bottom">
              <el-cascader
                ref="manageArea"
                v-model="areaId"
                :options="areasOptions"
                @change="getAreaId"
                clearable
                filterable
                :props="{ checkStrictly: true }"
                :append-to-body="false"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6" v-if="$store.state.userType != 'DIRECTOR'">
            <el-form-item label="所属行业" class="row-padding-bottom">
              <el-select
                v-model="filterField.industryId"
                filterable
                placeholder="请选择"
                clearable
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="item in industryOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="项目名称" class="row-padding-bottom">
                <el-input
                  v-model="filterField.projectName"
                  placeholder="请输入项目名称"
                  clearable
                ></el-input>
              </el-form-item>
            </el-col>
          </template>

          <el-form-item label="">
            <a
              @click="toggleAdvanced"
              style="margin-left: 40px; color: #409eff; cursor: pointer"
              class="btnColor"
            >
              {{ advanced ? '收起' : '展开' }}
              <i
                :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
              ></i>
            </a>
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchList"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
            <!-- <el-button
                class="commonBut saveBtn funComBut"
                v-if="showAdd"
                @click="toAdd('LABOR_ADMIN')"
              >
                <span class="opetareText">添加劳资专管员</span>
              </el-button>
              <el-button
                class="commonBut saveBtn funComBut"
                v-if="showAdd"
                @click="toAdd('WAGE_SUPERVISOR')"
              >
                <span class="opetareText">添加工资专管员</span>
              </el-button> -->
          </el-form-item>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <div style="margin-bottom: 24px" v-if="showAdd">
          <el-button
            class="commonBut saveBtn funComBut"
            @click="toAdd('LABOR_ADMIN')"
          >
            <span class="opetareText">添加劳资专管员</span>
          </el-button>
          <el-button
            class="commonBut saveBtn funComBut"
            @click="toAdd('WAGE_SUPERVISOR')"
          >
            <span class="opetareText">添加工资专管员</span>
          </el-button>
        </div>
        <el-table
          :data="tableList"
          ref="tableBox1"
          :max-height="tableHeight"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            prop="areaName"
            width="230"
            label="区域"
            :formatter="formatTd"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.areaName"
                :columnName="'areaName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="industryName"
            align="center"
            label="行业"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column prop="projectName" label="项目名称" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="roleName"
            align="center"
            label="角色"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="fiveSuperviseEmpVOList"
            label="姓名（联系方式）"
            align="center"
            :formatter="formatTd"
            min-width="160"
          >
            <template slot-scope="scope">
              <div
                v-for="(item, index) in scope.row.fiveSuperviseEmpVOList"
                :key="index"
              >
                {{ item.name }}({{ item.tel }})
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="left"
            :formatter="formatTd"
            fixed="right"
            width="130"
          >
            <template slot-scope="scope">
              <span v-if="showAdd">
                <el-link type="primary" @click="toEdit(scope.$index, scope.row)"
                  >修改</el-link
                >
                <el-link type="primary" @click="toDel(scope.$index, scope.row)"
                  >删除</el-link
                >
              </span>
              <span v-else>--</span>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关数据"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <!-- getTableList为获取子组件的数据 -->
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageNo="getpageNo"
          @getpageSize="getpageSize"
        ></Pagination>
      </div>
      <!-- 添加 addRegulationDialog -->
      <addRegulationDialog
        v-if="toAddRegulationVisible"
        :role="role"
        @closeDialog="closeDialog"
        :operate="operate"
        :detailArr="detailArr"
      ></addRegulationDialog>
    </div>
  </div>
</template>

<script>
import addRegulationDialog from '@/components/employee/addRegulationDialog'
import { mapActions, mapState } from 'vuex'
export default {
  name: 'companyRegulation',
  components: {
    addRegulationDialog,
  },
  data() {
    return {
      hiddleToolBar: false,
      tableHeight: null,
      titleText: '企业监管列表',
      advanced: false,
      //表格数据
      tableList: [],
      requestUrl: 'api-e/fivesupervise/getListByPage',
      //搜索条件
      filterField: {
        //用来区别是企业监管还是区域监管,企业监管列表为LABOR_ADMIN,WAGE_SUPERVISOR区域监管传值为LABOR_SECURITY_INSPECTOR,LABOR_LAW_SUPERVISOR,PROJECT_SUPERVISOR
        roles: 'LABOR_ADMIN,WAGE_SUPERVISOR',
        projectName: '', //项目名称
        industryId: '', //行业id
        areaId: '', //区域id
      },
      isShow: false, //列表加载
      loading: true,
      total: 0,
      pageSizeNumber: 20,
      pageNo: 1,
      //   新加字段
      toAddRegulationVisible: false,
      loginType: '',
      showAdd: '',
      role: '',
      operate: '',
      detailArr: '',
    }
  },
  computed: { ...mapState(['areasOptions', 'industryOptions']) },

  created() {
    this.loginType = localStorage.getItem('loginType')
    if (this.loginType == 'XM') {
      this.showAdd = true
    }
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
  },
  activated() {
    window.addEventListener('scroll', this.handleScroll, true)
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNum, this.pageSize)
      this.$refs.tableBox1.doLayout()
    })
  },
  deactivated() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  beforeDestroy() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleScroll(e) {
      this.$nextTick(function () {
        if (
          this.$refs.tableBox &&
          this.$refs.tableBox.getBoundingClientRect().top < 128
        ) {
          // this.tableHeight = this.$refs.mainCon.getBoundingClientRect().height - 84
          this.tableHeight = window.innerHeight - 246

          if (document.getElementsByClassName('tableBoxCur').length) {
            document.getElementsByClassName('tableBoxCur')[0].style.height =
              null
          }
          this.hiddleToolBar = true
        } else {
          this.hiddleToolBar = false
        }
      })
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNum = val
    },
    ...mapActions(['getAreas', 'getIndustry']),
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
    },
    // 删除
    toDel(index, row) {
      this.$confirm('您确认删除选中的记录吗?', '提示', {
        cancelButtonClass: 'btn-custom-cancel',
        type: 'warning',
      })
        .then(() => {
          this.del(row)
        })
        .catch(() => {})
    },
    del(row) {
      this.axios({
        method: 'post',
        url: 'api-e/fivesupervise/deleteRole',
        data: {
          id: row.id,
          batch: row.batch,
        },
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.$message({
                message: '删除成功',
                type: 'success',
              })
              this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
            } else {
              this.$message({
                message: response.data.errorMsg,
                type: 'error',
              })
            }
          },
          (response) => {}
        )
        .catch((error) => {})
    },
    //编辑
    toEdit(index, row) {
      this.toAddRegulationVisible = true
      this.operate = 'edit'
      this.role = row.role
      this.detailArr = row
    },
    toAdd(role) {
      this.role = role
      this.operate = 'add'
      this.toAddRegulationVisible = true
    },
    closeDialog(val) {
      this.toAddRegulationVisible = false
      if (val == 'success') {
        this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
      }
    },
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll, true)
    let that = this
    this.$nextTick(function () {
      let tableHeightCur = this.$refs.tableBox1.$el.clientHeight
      let tableTop = this.$refs.tableBox.getBoundingClientRect().top
      if (tableHeightCur + tableTop > window.innerHeight) {
        this.tableHeight =
          this.$refs.mainCon.getBoundingClientRect().height - 86
      } else {
        document.getElementsByClassName('tableBoxCur').length &&
          (document.getElementsByClassName('tableBoxCur')[0].style.height =
            window.innerHeight - tableTop - 28 + 'px')
      }

      window.onresize = function () {
        if (!that.$refs.tableBox1) {
          return
        }
        let tableHeightCur = that.$refs.tableBox1.$el.clientHeight
        let tableTop = that.$refs.tableBox.getBoundingClientRect().top
        if (tableHeightCur + tableTop > window.innerHeight) {
          that.tableHeight =
            that.$refs.mainCon.getBoundingClientRect().height - 86
        } else {
          document.getElementsByClassName('tableBoxCur').length &&
            (document.getElementsByClassName('tableBoxCur')[0].style.height =
              window.innerHeight - tableTop - 40 + 'px')
        }
      }
    })
  },
}
</script>

<style scoped lang="less">
@import '../../../static/css/filterArea.less';
</style>
